package com.stox.client.ui.javafx.screener.view;

import java.util.UUID;

import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;

import com.stox.client.api.core.Client;
import com.stox.client.ui.javafx.core.util.CSS;
import com.stox.client.ui.javafx.core.util.UI;
import com.stox.client.ui.javafx.screener.widget.ScanResultPanel;
import com.stox.client.ui.javafx.screener.widget.ScreenChooserPopupControl;
import com.stox.client.ui.javafx.screener.widget.ScreenerConfigurationPanel;
import com.stox.client.ui.javafx.widget.ConfigurationPopupControl;
import com.stox.client.ui.javafx.widget.auto.BeanAutoUI;
import com.stox.client.ui.javafx.workbench.stage.StoxStage;
import com.stox.client.ui.javafx.workbench.view.View;
import com.stox.shared.core.i18n.MessageSource;
import com.stox.shared.screener.model.ScreenBeanDescriptor;
import com.stox.shared.screener.model.ScreenerConfiguration;

public class ScreenerView extends View {

	private final TabPane tabPane = new TabPane();
	private ScreenChooserPopupControl screenChooserPopupControl;
	private final Button screensButton = new Button(MessageSource.get("Screens"));
	private final ScreenerConfiguration screenerConfiguration = new ScreenerConfiguration();

	public ScreenerView() {
		super(MessageSource.get("Screener"));
		UI.style(this, CSS.screenerView);
		UI.style(screensButton, CSS.appsButton);
		getTitleBar().getRight().getChildren().add(0, screensButton);
		screensButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(final ActionEvent event) {
				if (null == screenChooserPopupControl) {
					screenChooserPopupControl = new ScreenChooserPopupControl(ScreenerView.this);
				}
				screenChooserPopupControl.show(StoxStage.INSTANCE);
			}
		});
		setCenter(tabPane);

		setSize();
		setLocation();
	}

	@Override
	public void configure() {
		final ScreenerConfigurationPanel screenerConfigurationPanel = new ScreenerConfigurationPanel();
		screenerConfigurationPanel.populate(screenerConfiguration);
		final ConfigurationPopupControl configurationPopupControl = new ConfigurationPopupControl(screenerConfigurationPanel);
		configurationPopupControl.getApplyButton().setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(final ActionEvent event) {
				screenerConfigurationPanel.value(screenerConfiguration);
			}
		});
		configurationPopupControl.show(StoxStage.INSTANCE);
	}


	private void setSize() {
		setPrefHeight(StoxStage.INSTANCE.getContentPane().getHeight() / 2);
		setPrefWidth((StoxStage.INSTANCE.getContentPane().getWidth() * 2) / 5);
	}

	private void setLocation() {
		final Bounds bounds = StoxStage.INSTANCE.getContentPane().getLayoutBounds();
		setLayoutX(bounds.getMaxX() / 5);
		setLayoutY(bounds.getMaxY() - getPrefHeight());
	}

	public void addScreenBeanDescriptor(final ScreenBeanDescriptor screenBeanDescriptor) {
		final BeanAutoUI beanAutoUI = new BeanAutoUI(screenBeanDescriptor);
		final ConfigurationPopupControl configurationPopupControl = new ConfigurationPopupControl(beanAutoUI);
		configurationPopupControl.getApplyButton().setText(MessageSource.get("Start"));
		configurationPopupControl.getApplyButton().setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(final ActionEvent event) {
				beanAutoUI.setValue();
				final String scanId = UUID.randomUUID().toString();
				Client.SCREENER.start(scanId, screenerConfiguration, screenBeanDescriptor);
				final ScanResultPanel scanResultPanel = new ScanResultPanel(scanId, screenBeanDescriptor);

				final Tab tab = new Tab(screenBeanDescriptor.getDisplayName());
				tabPane.getTabs().add(tab);
				tab.setContent(scanResultPanel);
				tab.setOnCloseRequest(new EventHandler<Event>() {
					@Override
					public void handle(final Event event) {
						scanResultPanel.cancel(scanId);
					}
				});
			}
		});

		configurationPopupControl.show(StoxStage.INSTANCE);
	}

	public ScreenerConfiguration getScreenerConfiguration() {
		return screenerConfiguration;
	}

}
